package com.aistar.service.redis_collect_liked.impl;

import com.aistar.mapper.TravCollectMapper;
import com.aistar.mapper.TravelnoteMapper;
import com.aistar.pojo.TravCollect;
import com.aistar.pojo.Travelnote;
import com.aistar.service.redis_collect.RedisCollectService;
import com.aistar.service.redis_collect_liked.LikedCollectService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @author shkstart
 * @date 2019/10/16 - 2:52
 */
    @Service("LikedCollectServiceImpl")
    @Slf4j
    public class LikedCollectServiceImpl implements LikedCollectService {

    @Autowired
    TravCollectMapper travCollectMapper;

    @Qualifier("RedisCollectServiceImpl")
    @Autowired
    RedisCollectService redisService;

    @Autowired
    TravelnoteMapper travelnoteMapper;
//        @Autowired
//        UserService userService;

    /**
     * 保存点赞记录
     * @param userLike
     * @return
     */
        @Override
        @Transactional
        public int save(TravCollect userLike) {
            return travCollectMapper.insert(userLike);
        }

    /**
     * 批量保存或修改
     * @param list
     */
        @Override
        @Transactional
        public int saveAll(List<TravCollect> list) {
            Iterator<TravCollect> iterator = list.iterator();
            try{
                while (iterator.hasNext()){
                TravCollect travCollect = iterator.next();
                travCollect.setCollectTime(new Date());
                travCollectMapper.insert(travCollect);
                }
            }
           catch (Exception e){
                return -1;
           }
            return 1;
        }

    /**
     * 根据被点赞人的id查询点赞列表（即查询都谁给这个人点赞过）
     * @param likedUserId 被点赞人的id
     * @param pageable
     * @return
     */
        @Override
        public Page<TravCollect> getLikedListByLikedUserId(String likedUserId, Pageable pageable) {

            return null;
        }

    /**
     * 根据点赞人的id查询点赞列表（即查询这个人都给谁点赞过）
     * @param likedPostId
     * @param pageable
     * @return
     */
        @Override
        public Page<TravCollect> getLikedListByLikedPostId(String likedPostId, Pageable pageable) {
            return null;
        }

    /**
     * 通过被点赞人和点赞人id查询是否存在点赞记录
     * @param likedUserId
     * @param likedPostId
     * @return
     */
        @Override
        public TravCollect getByLikedUserIdAndLikedPostId(String likedUserId, String likedPostId) {
            return null;
        }

    /**
     * 将Redis里的点赞数据存入数据库中
     */
        @Override
        @Transactional
        public void transLikedFromRedis2DB() {
            int i1 = 0;
            List<TravCollect> list = redisService.getLikedDataFromRedis();
            System.out.println("list:"+list);
            Iterator<TravCollect> iterator = list.iterator();
            while (iterator.hasNext()) {
                TravCollect like = iterator.next();
                TravCollect ul = travCollectMapper.selectByPrimaryKey(like.getCollectTravId(),like.getCollectUserId());
                if (ul == null){
//                    1、首先更新数据到收藏表
                    if(like.getCollectStatus() == 1){
                        System.out.println("开始插入。。。");
                        like.setCollectTime(new Date());
                        //没有记录，直接存入
                        System.out.println("存入结果："+save(like));
//                        测试数据是否存到收藏表
                        Travelnote travelnote = travelnoteMapper.selectByPrimaryKey(like.getCollectTravId());

//                        2、增加travelnote表的收藏量
                        if(travelnote.getTravOther2() == null){
                            i1 = 1;
                        }
                        else {
                            i1= Integer.parseInt(travelnote.getTravOther2())+1;
                        }
                        String collect_num = String.valueOf(i1);
                        travelnote.setTravOther2(collect_num);
                        travelnoteMapper.updateByPrimaryKey(travelnote);
                        System.out.println("travelnote_10_17:"+travelnoteMapper.selectByPrimaryKey(like.getCollectTravId()).getTravOther2());
                    }
                }else{
//                    1、首先更新数据到收藏表
                    System.out.println("找到了："+ul);
                    System.out.println("ul:"+ul.getCollectStatus());
                    //有记录，需要更新
                    int status = like.getCollectStatus();
                    ul.setCollectStatus(status);
//                    ul.setCollectTime(ul.getCollectTime());
                    ul.setCollectUpdateTime(new Date());
                    System.out.println("更新后的ul对象："+ul);
                    int result = travCollectMapper.updateByPrimaryKey(ul);
                    System.out.println("更新结果："+result);
                    System.out.println("跟新后的结果："+ul);
//                    2、然后修改travelnote表的收藏量
                    if(ul.getCollectStatus()==0){
                        Travelnote travelnote = travelnoteMapper.selectByPrimaryKey(ul.getCollectTravId());
                        int i= Integer.parseInt(travelnote.getTravOther2())-1;
                        String collect_num = String.valueOf(i);
                        travelnote.setTravOther2(collect_num);
                        travelnoteMapper.updateByPrimaryKey(travelnote);
                        System.out.println("travelnote_10_17:"+travelnoteMapper.selectByPrimaryKey(ul.getCollectTravId()).getTravOther2());
                    }
                    else if(ul.getCollectStatus()==1){
                        Travelnote travelnote = travelnoteMapper.selectByPrimaryKey(ul.getCollectTravId());
                        int i= Integer.parseInt(travelnote.getTravOther2())+1;
                        String collect_num = String.valueOf(i);
                        travelnote.setTravOther2(collect_num);
                        travelnoteMapper.updateByPrimaryKey(travelnote);
                        System.out.println("travelnote_10_17:"+travelnoteMapper.selectByPrimaryKey(ul.getCollectTravId()).getTravOther2());
                    }
                }
            }
        }

    /**
     * 将Redis中的点赞数量数据存入数据库
     */
//        @Override
//        @Transactional
//        public void transLikedCountFromRedis2DB() {
//            List<Travelnote> list = redisService.getLikedCountFromRedis();
//            System.out.println("travelnotes:"+list);
//            for (Travelnote dto : list) {
//                Travelnote travelnote = travelnoteMapper.selectByPrimaryKey(dto.getTravId());
//                System.out.println("travelnote:"+travelnote.getTravOther2());
//                //点赞数量属于无关紧要的操作，出错无需抛异常
//                if (travelnote != null){
//                  int i =  Integer.parseInt(travelnote.getTravOther2())+dto.getTravPraise();
//                    travelnote.setTravOther2(String.valueOf(i));
//                    //更新点赞数量
//                    int val = travelnoteMapper.updateByPrimaryKey(travelnote);
//                    System.out.println("val:"+val);
//                    System.out.println(travelnoteMapper.selectByPrimaryKey(travelnote.getTravId()));
//                }
//            }
//        }
    }

